<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Archive extends CI_Controller {
	
	function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * 档案入口, this is a test
	 */
	public function index()
	{
		$this->idcard();
	}
	
	/**
	 * 1.身份证信息
	 */
	public function idcard($id=null){
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$info = $this->Archive_Idcard_Model->get_one($id);
			
			if ( empty($info) ){
				show_error('您查询的客户编号不存在!' );
			}
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
        $ethnic = $this->db->query("select * from visa_ethnic order by en_name")->result();
		$this->config->load('my_options', TRUE); // added by zhouhuan, 2012.10.9 
		$data = array(
			'id' => $id,
			'info' => $info,
			'forms' => $filled,
            'ethnic' => $ethnic,
            'visa_purpose_array' => $this->config->item('visa_purpose', 'my_options') 
		);
		//print_r($data);
		$this->load->view('archive_idcard', $data);
	}
	
	/**
	 * 1.身份证信息更新
	 */
	public function idcard_update(){
		//print_r($this->input->post());		
		$surname = $this->input->post('surname');
		$firstname = $this->input->post('firstname');
		$sex = $this->input->post('sex');
		$national_id = $this->input->post('national_id');
		$id = $this->input->post('id');
		$isExist = (empty($id))?FALSE:TRUE;
		
		if ( empty($surname) || empty($firstname) || empty($sex) || empty($national_id) ){
			show_error('參數錯誤，請查證後提交' );
		} 

		$this->load->model('Archive_Idcard_Model','',TRUE);
		$data = $this->input->post();
		$data['create_date'] = date("Y-m-d H:i:s");
		if ( $isExist ){	//insert or update
			$this->Archive_Idcard_Model->update_entry($id, $data);
		}else{
			unset ($data['id']);
			$id = $this->Archive_Idcard_Model->insert_entry($data);
            // 写入一条用户关联记录; added by zhouhuan, 2012.9.26
            session_start();
            $record = array();
            $record['card_id'] = $id;
            $record['user_id'] = $_SESSION['user_id'];
            $record['visa_type'] = $_SESSION['visa_type'];  // 签证业务类别
            $record['visa_purpose'] = $_SESSION['visa_purpose'];  // 签证用途
            $record['province_name'] = $_SESSION['province_name'];   // 户口所在地    
            $record['country_ids'] = $_SESSION['country_ids'];   // 国家id串
            $record['country_cn_names'] = $_SESSION['country_cn_names'];  // 国家中文名(一个或多个)
            $record['country_short_names'] = $_SESSION['country_short_names']; // 国家英文简称(一个或多个)
            $record['country_schengen_names'] = $_SESSION['country_schengen_names'];  // 申根国家中文名(一个或多个) 
            $record['main_schengen_country'] = $_SESSION['main_schengen_country'];    // 申根主签国中文名(Only One)  
            $record['create_time'] = date("Y-m-d H:i:s");
            $record['update_time'] = date("Y-m-d H:i:s"); 
            $this->db->insert('visa_user_forms', $record);
		}
		redirect('archive/idcard/'.$id);
	}
	
	/**
	 * 2.户口本
	 */
	public function residency($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Residency_Model','',TRUE);
			$info = $this->Archive_Residency_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->name = $idcard->surname.' '.$idcard->firstname;
			$info->sex = $idcard->sex;
			$info->birth = $idcard->birth_m.'/'.$idcard->birth_d.'/'.$idcard->birth_y;
		}
        $ethnic = $this->db->query("select * from visa_ethnic order by en_name")->result();  
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
            'ethnic' => $ethnic,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_residency', $data);
	}
	
	/**
	 * 2.户口本更新
	 */
	public function residency_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Residency_Model','',TRUE);
		$info = $this->Archive_Residency_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Residency_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Residency_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/residency/'.$id.'/'.$cateid);
	}
	
	/**
	 * 3.护照
	 */
	public function passport($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Passport_Model','',TRUE);
			$info = $this->Archive_Passport_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->surname = $idcard->surname;
			$info->firstname = $idcard->firstname;
			$info->sex = $idcard->sex;
			$info->birth = $idcard->birth_d.'/'.$idcard->birth_m.'/'.$idcard->birth_y;
			$info->idcard = $idcard->national_id;
			$info->country_code = 'CHN';
			$info->passport_type = 'P';
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_passport', $data);
	}
	
	/**
	 * 3.护照更新
	 */
	public function passport_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Passport_Model','',TRUE);
		$info = $this->Archive_Passport_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Passport_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Passport_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
        session_start();
        $_SESSION['passport_no'] = $_POST['passport_no'];
		redirect('archive/passport/'.$id.'/'.$cateid);
	}
	
	/**
	 * 4.退休证
	 */
	public function retire($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Retire_Model','',TRUE);
			$info = $this->Archive_Retire_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->name = $idcard->surname.' '.$idcard->firstname;
			$info->sex = $idcard->sex;
			$info->birth = $idcard->birth_m.'/'.$idcard->birth_d.'/'.$idcard->birth_y;
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_retire', $data);
	}
	
	/**
	 * 4.退休证更新
	 */
	public function retire_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Retire_Model','',TRUE);
		$info = $this->Archive_Retire_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Retire_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Retire_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/retire/'.$id.'/'.$cateid);
	}
	
	/**
	 * 5.结婚证
	 */
	public function marriage($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Marriage_Model','',TRUE);
			$info = $this->Archive_Marriage_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->holder = $idcard->surname.' '.$idcard->firstname;
			$info->name1 = $idcard->surname.' '.$idcard->firstname;
			$info->sex1 = $idcard->sex;
			$info->birth1 = $idcard->birth_m.'/'.$idcard->birth_d.'/'.$idcard->birth_y;
			$info->idcard1 = $idcard->national_id;
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_marriage', $data);
	}
	
	/**
	 * 5.结婚证更新
	 */
	public function marriage_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Marriage_Model','',TRUE);
		$info = $this->Archive_Marriage_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Marriage_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Marriage_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/marriage/'.$id.'/'.$cateid);
	}
	
	/**
	 * 6.离婚证
	 */
	public function divorce($id, $cateid){
          !(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
        
        $info = null;
        $filled = null;
        if ( !empty($id) ){
            $query = $this->db->query("select * from archive_divorce where id=$id");
            if($query->num_rows() >0) {
                $info = $query->row();   
            }
            $this->load->library('fillform',array('id'=>$id));
            $filled = $this->fillform->getFilled();
        }
        //autofill
        if( empty($info) ){
            $info = (object) $info;
            $idcard = $this->fillform->getIdCard();
            $info->full_name = $idcard->surname.' '.$idcard->firstname;
            $info->name_1 = $idcard->surname.' '.$idcard->firstname;
            $info->sex = $idcard->sex;
            $info->birth_1 = $idcard->birth_m.'/'.$idcard->birth_d.'/'.$idcard->birth_y;
        }
        $data = array(
            'id' => $id,
            'cateid' => $cateid,
            'info' => $info,
            'forms' => $filled,
        );
        //print_r($data);
        $this->load->view('archive_divorce', $data);
	}
	
	/**
	 * 6.离婚证更新
	 */
	public function divorce_update($cateid){
        !empty($cateid) || show_error('參數錯誤，请查证后再试' );
        
        //print_r($this->input->post());        
        $id = $this->input->post('id');

        $query = $this->db->query("select * from archive_divorce where id=$id");
        $isExist = ($query->num_rows() >0); 

        if ( $isExist ){    //insert or update
            $this->db->update('archive_divorce', $this->input->post(), array('id' => $id));
        }else{
            $this->db->insert('archive_divorce', $this->input->post());    
            //update forms 
            $this->load->model('Archive_Idcard_Model','',TRUE);
            $this->Archive_Idcard_Model->update_forms($id, $cateid);
        }
        redirect('archive/divorce/'.$id.'/'.$cateid);
	}
	
	/**
	 * 7.房产证
	 */
	public function property($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Property_Model','',TRUE);
			$info = $this->Archive_Property_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->owner = $idcard->surname.' '.$idcard->firstname;
			$info->address = $idcard->home_address;
			$info->clasification = 'Privately owned house';
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_property', $data);
	}
	
	/**
	 * 7.房产证更新
	 */
	public function property_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Property_Model','',TRUE);
		$info = $this->Archive_Property_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Property_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Property_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/property/'.$id.'/'.$cateid);
	}

	/**
	 * 8.车产证
	 */
	public function auto($id, $cateid){
		die("制作中");
	}
	
	/**
	 * 8.车产证更新
	 */
	public function auto_update($cateid){
		die("制作中");
	}
	
	/**
	 * 9.驾驶证
	 */
	public function driving($id, $cateid){
		die("制作中");
	}
	
	/**
	 * 9.驾驶证更新
	 */
	public function driving_update($cateid){
		die("制作中");
	}

	/**
	 * 10.行驶证
	 */
	public function drive($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Drive_Model','',TRUE);
			$info = $this->Archive_Drive_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->owner = $idcard->surname.' '.$idcard->firstname;
			$info->address = $idcard->home_address;
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_drive', $data);
	}
	
	/**
	 * 10.行驶证更新
	 */
	public function drive_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Drive_Model','',TRUE);
		$info = $this->Archive_Drive_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Drive_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Drive_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/drive/'.$id.'/'.$cateid);
	}
	
	/**
	 * 11.机动车注册摘要信息栏
	 */
	public function autoinfo($id, $cateid){
		die("制作中");
	}
	
	/**
	 * 11.机动车注册摘要信息栏更新
	 */
	public function autoinfo_update($cateid){
		die("制作中");
	}
	
	/**
	 * 12.存款证明
	 */
	public function cd($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Cd_Model','',TRUE);
			$info = $this->Archive_Cd_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->depositor = $idcard->surname.' '.$idcard->firstname;
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_cd', $data);
	}
	
	/**
	 * 12.存款证明更新
	 */
	public function cd_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Cd_Model','',TRUE);
		$info = $this->Archive_Cd_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Cd_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Cd_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/cd/'.$id.'/'.$cateid);
	}
	
	/**
	 * 13.在职证明
	 */
	public function employment($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Employment_Model','',TRUE);
			$info = $this->Archive_Employment_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
            session_start();
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->name = $idcard->surname.' '.$idcard->firstname;
			$info->sex = $idcard->sex;
			$info->birth = $idcard->birth_m.'/'.$idcard->birth_d.'/'.$idcard->birth_y;
            $arr = array();
            $result = $this->db->query("select en_name from visa_countries where id in (".$_SESSION['country_ids'].")")->result();
            foreach($result as $row) {
                $arr[] = $row->en_name;
            }
            $info->dest_country = join(',', $arr);
		}                                
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_employment', $data);
	}
	
	/**
	 * 13.在职证明更新
	 */
	public function employment_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Employment_Model','',TRUE);
		$info = $this->Archive_Employment_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Employment_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Employment_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/employment/'.$id.'/'.$cateid);
	}
	
	
	/**
	 * 14.收入证明
	 */
	public function income($id, $cateid){
          !(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
        
        $info = null;
        $filled = null;
        if ( !empty($id) ){
            $query = $this->db->query("select * from archive_income where id=$id");
            if($query->num_rows() >0) {
                $info = $query->row();   
            }
            $this->load->library('fillform',array('id'=>$id));
            $filled = $this->fillform->getFilled();
        }
        //autofill
        if( empty($info) ){
            $info = (object) $info;
            $idcard = $this->fillform->getIdCard();
            $info->full_name = $idcard->surname.' '.$idcard->firstname;
        }
        $data = array(
            'id' => $id,
            'cateid' => $cateid,
            'info' => $info,
            'forms' => $filled,
        );
        //print_r($data);
        $this->load->view('archive_income', $data);
	}
	
	/**
	 * 14.收入证明更新
	 */
	public function income_update($cateid){
        !empty($cateid) || show_error('參數錯誤，请查证后再试' );
        
        //print_r($this->input->post());        
        $id = $this->input->post('id');

        $query = $this->db->query("select * from archive_income where id=$id");
        $isExist = ($query->num_rows() >0); 

        if ( $isExist ){    //insert or update
            $this->db->update('archive_income', $this->input->post(), array('id' => $id));
        }else{
            $this->db->insert('archive_income', $this->input->post());    
            //update forms 
            $this->load->model('Archive_Idcard_Model','',TRUE);
            $this->Archive_Idcard_Model->update_forms($id, $cateid);
        }
        redirect('archive/income/'.$id.'/'.$cateid);
	}
	
	/**
	 * 15.营业执照
	 */
	public function business($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Business_Model','',TRUE);
			$info = $this->Archive_Business_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
		//autofill
		if( empty($info) ){
			$info = (object) $info;
			$idcard = $this->fillform->getIdCard();
			$info->legal_representative = $idcard->surname.' '.$idcard->firstname;
		}
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_business', $data);
	}
	
	/**
	 * 15.营业执照更新
	 */
	public function business_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Business_Model','',TRUE);
		$info = $this->Archive_Business_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Business_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Business_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/business/'.$id.'/'.$cateid);
	}
	
	/**
	 * 16.组织机构代码
	 */
	public function osnumber($id, $cateid){
		!(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
		
		$info = null;
		$filled = null;
		if ( !empty($id) ){
			$this->load->model('Archive_Osnumber_Model','',TRUE);
			$info = $this->Archive_Osnumber_Model->get_one($id);
			$this->load->library('fillform',array('id'=>$id));
			$filled = $this->fillform->getFilled();
		}
	
		$data = array(
			'id' => $id,
			'cateid' => $cateid,
			'info' => $info,
			'forms' => $filled,
		);
		//print_r($data);
		$this->load->view('archive_osnumber', $data);
	}
	
	/**
	 * 16.组织机构代码更新
	 */
	public function osnumber_update($cateid){
		!empty($cateid) || show_error('參數錯誤，请查证后再试' );
		
		//print_r($this->input->post());		
		$id = $this->input->post('id');

		$this->load->model('Archive_Osnumber_Model','',TRUE);
		$info = $this->Archive_Osnumber_Model->get_one($id);
		$isExist = (empty($info))?FALSE:TRUE;

		if ( $isExist ){	//insert or update
			$this->Archive_Osnumber_Model->update_entry($id, $this->input->post());
		}else{
			$this->Archive_Osnumber_Model->insert_entry($this->input->post());
			//update forms 
			$this->load->model('Archive_Idcard_Model','',TRUE);
			$this->Archive_Idcard_Model->update_forms($id, $cateid);
		}
		redirect('archive/osnumber/'.$id.'/'.$cateid);
	}

	/**
	 * 17.父母授权书
	 */
	public function parents($id, $cateid){
          !(empty($id) || empty($cateid)) || show_error('參數錯誤，请查证后再试' );
        
        $info = null;
        $filled = null;
        if ( !empty($id) ){
            $query = $this->db->query("select * from archive_parents where id=$id");
            if($query->num_rows() >0) {
                $info = $query->row();   
            }
            $this->load->library('fillform',array('id'=>$id));
            $filled = $this->fillform->getFilled();
        }
        //autofill
        if( empty($info) ){
            $info = (object) $info;
            $idcard = $this->fillform->getIdCard();
            $info->full_name = $idcard->surname.' '.$idcard->firstname;
            $info->birth = $idcard->birth_m.'/'.$idcard->birth_d.'/'.$idcard->birth_y;
        }
        $data = array(
            'id' => $id,
            'cateid' => $cateid,
            'info' => $info,
            'forms' => $filled,
        );
        //print_r($data);
        $this->load->view('archive_parents', $data);
	}
	
	/**
	 * 17.父母授权书更新
	 */
	public function parents_update($cateid){
        !empty($cateid) || show_error('參數錯誤，请查证后再试' );
        
        //print_r($this->input->post());        
        $id = $this->input->post('id');

        $query = $this->db->query("select * from archive_parents where id=$id");
        $isExist = ($query->num_rows() >0); 

        if ( $isExist ){    //insert or update
            $this->db->update('archive_parents', $this->input->post(), array('id' => $id));
        }else{
            $this->db->insert('archive_parents', $this->input->post());    
            //update forms 
            $this->load->model('Archive_Idcard_Model','',TRUE);
            $this->Archive_Idcard_Model->update_forms($id, $cateid);
        }
        redirect('archive/parents/'.$id.'/'.$cateid);
	}

}

/* End of file archive.php */
/* Location: ./application/controllers/archive.php */